package com.isoft.illustrationsdraw.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.isoft.illustrationsdraw.brief.BriefIllustrationInfo;
import com.isoft.illustrationsdraw.brief.BriefUserInfo;
import com.isoft.illustrationsdraw.entity.Illustration;
import com.isoft.illustrationsdraw.entity.IllustrationTag;
import com.isoft.illustrationsdraw.entity.User;
import com.isoft.illustrationsdraw.mapper.IllustrationTagMapper;
import com.isoft.illustrationsdraw.service.IllustrationTagService;
import com.isoft.illustrationsdraw.service.SearchService;
import com.isoft.illustrationsdraw.service.TagService;
import com.isoft.illustrationsdraw.service.UserService;
import com.isoft.illustrationsdraw.service.FileService;
import com.isoft.illustrationsdraw.service.IllustrationCollectionService;
import com.isoft.illustrationsdraw.service.IllustrationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.illustrationsdraw.mapper.IllustrationMapper;
import com.isoft.illustrationsdraw.service.LikeIllustrationService;

@Service
public class SearchServiceImpl implements SearchService {

    private final TagService tagService;
    private final IllustrationCollectionService illustrationCollectionService;
    private final IllustrationService illustrationService;
    private final FileService fileService;
    private final UserService userService;
    private final IllustrationTagMapper illustrationTagMapper;
    private final IllustrationMapper illustrationMapper;
    private final LikeIllustrationService likeIllustrationService;

    public SearchServiceImpl(TagService tagService, IllustrationTagService illustrationTagService, IllustrationCollectionService illustrationCollectionService, IllustrationService illustrationService, FileService fileService, UserService userService, IllustrationTagMapper illustrationTagMapper, IllustrationMapper illustrationMapper, LikeIllustrationService likeIllustrationService) {
        this.tagService = tagService;
        this.illustrationService = illustrationService;
        this.illustrationCollectionService = illustrationCollectionService;
        this.fileService = fileService;
        this.userService = userService;
        this.illustrationTagMapper = illustrationTagMapper;
        this.illustrationMapper = illustrationMapper;
        this.likeIllustrationService = likeIllustrationService;
    }


    @Override
    public List<BriefIllustrationInfo> searchIllustrationByTag(Long userId, String tagName, Long tagIdA, Integer page) {
        Long tagId = tagIdA == null ? tagService.getTagIdByName(tagName) : tagIdA;
        Page<IllustrationTag> illustrationTagPage = new Page<>(page, 20);

        LambdaQueryWrapper<IllustrationTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IllustrationTag::getTagId, tagId);
        
        var illustrationTags = illustrationTagMapper.selectPage(illustrationTagPage, queryWrapper).getRecords();
        
        var result = new ArrayList<BriefIllustrationInfo>();

        for (var i : illustrationTags) {
            
            Illustration info = illustrationService.getById(i.getIllustrationsId());
            User author = userService.getById(info.getAuthorId());
            var url = fileService.convertToOnlinePath(illustrationCollectionService.getCollectionByIllustrationId(i.getIllustrationsId()).get(0).getImageUrl());
            boolean isLiked = likeIllustrationService.findLikeIllustration(userId, info.getId()) == null ? false : true;

            result.add(new BriefIllustrationInfo(info.getId(), info.getTitle(), url, new BriefUserInfo(author.getId(), author.getNickname(), author.getProfileImg() == null ? null : fileService.convertToOnlinePath(author.getProfileImg())), isLiked));
        }

        return result;
    }

    @Override
    public List<BriefIllustrationInfo> searchIllustrationByKeyworks(Long userId, String keywords, Integer page) {
        QueryWrapper<Illustration> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keywords).or().like("description", keywords);
        Page<Illustration> illustrationPage = new Page<>(page, 20);

        var illustrations = illustrationMapper.selectPage(illustrationPage, queryWrapper).getRecords();

        var result = new ArrayList<BriefIllustrationInfo>();
        for (var info : illustrations) {
            User author = userService.getById(info.getAuthorId());
            var url = fileService.convertToOnlinePath(illustrationCollectionService.getCollectionByIllustrationId(info.getId()).get(0).getImageUrl());
            boolean isLiked = likeIllustrationService.findLikeIllustration(userId, info.getId()) == null ? false : true;
            result.add(new BriefIllustrationInfo(info.getId(), info.getTitle(), url, new BriefUserInfo(author.getId(), author.getNickname(), author.getProfileImg() == null ? null : fileService.convertToOnlinePath(author.getProfileImg())), isLiked));
        }
        return result;
    }
}
